//
// Created by root on 01/04/17.
//

#include <cstdio>
#include <Poco/File.h>
#include <fstream>
#include "img_svr_main.h"
#include "Poco/FileChannel.h"
#include "Poco/PatternFormatter.h"
#include "Poco/FormattingChannel.h"
#include "pic_store_interface.h"
#include "meta_svr_interface.h"

namespace img_svr{
    ImgSvrMain& ImgSvrMain::instance() {
        static ImgSvrMain singleton;
        return *(ImgSvrMain*)(&Poco::Util::Application::instance());
    }
    int ImgSvrMain::main(const std::vector<std::string> &args) {
        if(load_config()){
            printf("config init error\n");
            return -1;
        }
        init_log();
        meta_svr::MetaSvrInterface::SingletonInstance().Init(_sys_config.meta_svr_ip,_sys_config.meta_svr_port);
        pic_store_api::PicStoreInterface::SingletonInstance().Init(_sys_config.foshan_ip,_sys_config.jinzhong_ip,_sys_config.xianning_ip,_sys_config.yancheng_ip);
        _httpserver=new HttpSvr((uint16_t )_sys_config.http_port,_sys_config.http_thread_count);
        _httpserver->start();
        CleanTempFile::Instance().Init(_sys_config.img_root_path,_sys_config.store_days);
        CleanTempFile::Instance().Start();
    }

    void ImgSvrMain::init_log() {
        Poco::File f("logs");
        f.createDirectory();
        Poco::AutoPtr<Poco::FileChannel> pChnHttp(new Poco::FileChannel);
        pChnHttp->setProperty("path", "./logs/http.log");
        pChnHttp->setProperty("rotation", "256 M");
        pChnHttp->setProperty("archive", "timestamp");
        //pChannel->setProperty("compress", "true");
        pChnHttp->setProperty("purgeAge", "2 weeks");
        pChnHttp->setProperty("purgeCount", "10");
        pChnHttp->setProperty("times", "local");
        pChnHttp->setProperty("flush", "true");
        Poco::AutoPtr<Poco::PatternFormatter> pPFHttp(new Poco::PatternFormatter);
        pPFHttp->setProperty("times", "local");
        pPFHttp->setProperty("pattern", "%Y-%m-%d %H:%M:%S.%i %I: %t");
        Poco::AutoPtr<Poco::FormattingChannel> pFCHttp(new Poco::FormattingChannel(pPFHttp, pChnHttp));
        HttpLog.setChannel(pFCHttp);

        Poco::AutoPtr<Poco::FileChannel> pChnMeta(new Poco::FileChannel);
        pChnMeta->setProperty("path", "./logs/meta.log");
        pChnMeta->setProperty("rotation", "256 M");
        pChnMeta->setProperty("archive", "timestamp");
        //pChannel->setProperty("compress", "true");
        pChnMeta->setProperty("purgeAge", "2 weeks");
        pChnMeta->setProperty("purgeCount", "10");
        pChnMeta->setProperty("times", "local");
        pChnMeta->setProperty("flush", "true");
        Poco::AutoPtr<Poco::PatternFormatter> pPFMeta(new Poco::PatternFormatter);
        pPFMeta->setProperty("times", "local");
        pPFMeta->setProperty("pattern", "%Y-%m-%d %H:%M:%S.%i %I: %t");
        Poco::AutoPtr<Poco::FormattingChannel> pFCMeta(new Poco::FormattingChannel(pPFMeta, pChnMeta));
        MetaLog.setChannel(pFCMeta);

        Poco::AutoPtr<Poco::FileChannel> pChnDownload(new Poco::FileChannel);
        pChnDownload->setProperty("path", "./logs/download.log");
        pChnDownload->setProperty("rotation", "256 M");
        pChnDownload->setProperty("archive", "timestamp");
        //pChannel->setProperty("compress", "true");
        pChnDownload->setProperty("purgeAge", "2 weeks");
        pChnDownload->setProperty("purgeCount", "10");
        pChnDownload->setProperty("times", "local");
        pChnDownload->setProperty("flush", "true");
        Poco::AutoPtr<Poco::PatternFormatter> pPFDownload(new Poco::PatternFormatter);
        pPFDownload->setProperty("times", "local");
        pPFDownload->setProperty("pattern", "%Y-%m-%d %H:%M:%S.%i %I: %t");
        Poco::AutoPtr<Poco::FormattingChannel> pFCDownload(new Poco::FormattingChannel(pPFDownload, pChnDownload));
        DownloadLog.setChannel(pFCDownload);

        Poco::AutoPtr<Poco::FileChannel> pChnService(new Poco::FileChannel);
        pChnService->setProperty("path", "./logs/service.log");
        pChnService->setProperty("rotation", "256 M");
        pChnService->setProperty("archive", "timestamp");
        //pChannel->setProperty("compress", "true");
        pChnService->setProperty("purgeAge", "2 weeks");
        pChnService->setProperty("purgeCount", "10");
        pChnService->setProperty("times", "local");
        pChnService->setProperty("flush", "true");
        Poco::AutoPtr<Poco::PatternFormatter> pPFService(new Poco::PatternFormatter);
        pPFService->setProperty("times", "local");
        pPFService->setProperty("pattern", "%Y-%m-%d %H:%M:%S.%i %I: %t");
        Poco::AutoPtr<Poco::FormattingChannel> pFCService(new Poco::FormattingChannel(pPFService, pChnService));
        ServiceLog.setChannel(pFCService);

        Poco::AutoPtr<Poco::FileChannel> pChnCleanTemp(new Poco::FileChannel);
        pChnCleanTemp->setProperty("path", "./logs/clean.log");
        pChnCleanTemp->setProperty("rotation", "256 M");
        pChnCleanTemp->setProperty("archive", "timestamp");
        //pChannel->setProperty("compress", "true");
        pChnCleanTemp->setProperty("purgeAge", "2 weeks");
        pChnCleanTemp->setProperty("purgeCount", "10");
        pChnCleanTemp->setProperty("times", "local");
        pChnCleanTemp->setProperty("flush", "true");
        Poco::AutoPtr<Poco::PatternFormatter> pPFCleanTemp(new Poco::PatternFormatter);
        pPFCleanTemp->setProperty("times", "local");
        pPFCleanTemp->setProperty("pattern", "%Y-%m-%d %H:%M:%S.%i %I: %t");
        Poco::AutoPtr<Poco::FormattingChannel> pFCCleanTemp(new Poco::FormattingChannel(pPFCleanTemp, pChnCleanTemp));
        CleanTempLog.setChannel(pFCCleanTemp);

        Poco::AutoPtr<Poco::FileChannel> pChnErrReq(new Poco::FileChannel);
        pChnErrReq->setProperty("path", "./logs/err_req.log");
        pChnErrReq->setProperty("rotation", "256 M");
        pChnErrReq->setProperty("archive", "timestamp");
        //pChannel->setProperty("compress", "true");
        pChnErrReq->setProperty("purgeAge", "2 weeks");
        pChnErrReq->setProperty("purgeCount", "10");
        pChnErrReq->setProperty("times", "local");
        pChnErrReq->setProperty("flush", "true");
        Poco::AutoPtr<Poco::PatternFormatter> pPFErrReq(new Poco::PatternFormatter);
        pPFErrReq->setProperty("times", "local");
        pPFErrReq->setProperty("pattern", "%Y-%m-%d %H:%M:%S.%i %I: %t");
        Poco::AutoPtr<Poco::FormattingChannel> pFCErrReq(new Poco::FormattingChannel(pPFErrReq, pChnErrReq));
        ErrReqLog.setChannel(pFCErrReq);

    }
    int ImgSvrMain::load_config() {
        int ret=0;
        if(loadConfiguration()){
            _sys_config.http_port=config().getUInt("local.http_port",8080);
            _sys_config.http_thread_count=config().getInt("local.http_thread_count",100);
            _sys_config.img_root_path=config().getString("local.path_temp","/data12/img_path");
            _sys_config.nginx_port=config().getUInt("local.nginx_port",80);
            _sys_config.store_days=config().getInt("local.store_day",7);
            _sys_config.meta_svr_ip=config().getString("server.meta_svr_ip","116.31.112.83");
            _sys_config.meta_svr_port=config().getUInt("server.meta_svr_port",9090);
            _sys_config.foshan_ip=config().getString("server.foshan_ip","14.17.109.77");
            _sys_config.jinzhong_ip=config().getString("server.jinzhong_ip","61.240.41.129");
            _sys_config.xianning_ip=config().getString("server.xianning_ip","58.52.135.162");
            _sys_config.yancheng_ip=config().getString("server.yancheng_ip","114.236.142.225");
            get_local_ip(_sys_config.local_ip,"14.215.104.22");
        }else
        {
            ret=-1;
        }
        return ret;
    }
    int ImgSvrMain::get_local_ip(string &ip,string default_ip) {
        std::ifstream ifs("/data/zhaogang1/ip.txt");
        if(!ifs.is_open()){
            ip=default_ip;
            return -1;
        }else{
            char buf[32]={0};
            if(ifs.getline(buf,32)){
                ip=string(buf);
            }else{
                ip=default_ip;
                ifs.close();
                return -1;
            }
        }
        ifs.close();
        return 0;
    }
}

int main(int argc ,char** argv) {
 img_svr::ImgSvrMain::instance().run(argc,argv);
    pause();
}